using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Data.Service;
using ToDoList.Data;
using ToDo.Models;
using Microsoft.AspNetCore.Http;

namespace ToDo.Controllers
{
    public class ItemController : Controller
    {
        private ItemService _itemService = new ItemService();
        private InventoryService _inventoryService = new InventoryService();

        /// <summary>
        /// 展示未完成事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public IActionResult ShowNoFinishItems(int inventoryId)
        {
            List<ItemViewModels> noFinished = SaveInViewModels(_itemService.GetItemsInInventory(inventoryId));
            var noFinishItems = new ItemsListViewModel()
            {
                noFinished = noFinished,
                InventoryId = inventoryId
            };
            return View(noFinishItems);
        }
        /// <summary>
        /// 展示完成事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public IActionResult ShowFinishItems(int inventoryId)
        {
            List<ItemViewModels> hasFinished = SaveInViewModels(_itemService.GetFinishedItemsInInventory(inventoryId));
            return View(hasFinished);
        }
        /// <summary>
        /// 展示过期事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public IActionResult ShowOverdueItems(int inventoryId)
        {
            List<ItemViewModels> hasOverDue = SaveInViewModels(_itemService.GetOverdueItemsInInventory(inventoryId));
            return View(hasOverDue);
        }
        /// <summary>
        /// 查看所有未完成事项 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAllNoFinishItem()
        {
            List<ItemViewModels> itemsView = new List<ItemViewModels>();
            itemsView = SaveInViewModels(_itemService.GetAllNotFinishIteams((int)HttpContext.Session.GetInt32("userId")));

            return View(itemsView);
        }
        /// <summary>
        /// 查看所有完成事项 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAllFinishItem()
        {
            List<ItemViewModels> itemsView = new List<ItemViewModels>();
            itemsView = SaveInViewModels(_itemService.GetAllFinishIteams((int)HttpContext.Session.GetInt32("userId")));

            return View(itemsView);
        }
        /// <summary>
        /// 查看所有过期事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAllOverdueItem()
        {
            List<ItemViewModels> itemsView = new List<ItemViewModels>();
            itemsView = SaveInViewModels(_itemService.GetAllOverdueIteams((int)HttpContext.Session.GetInt32("userId")));

            return View(itemsView);
        }
        /// <summary>
        /// 删除事项 （从所有未完成的事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteItem(int itemId)
        {
            _itemService.DeleteItem(itemId);

            return RedirectToAction("ShowAllNoFinishItem");
        }

        /// <summary>
        /// 删除事项（从所有完成事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public IActionResult DeleteFinishItem(int itemId)
        {
            _itemService.DeleteItem(itemId);

            return RedirectToAction("ShowAllFinishItem");
        }

        /// <summary>
        /// 删除事项（从所有过期事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public IActionResult DeleteOverdueItem(int itemId)
        {
            _itemService.DeleteItem(itemId);

            return RedirectToAction("ShowAllOverdueItem");
        }

        /// <summary>
        /// 删除事项（从某一个清单的事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public JsonResult DeleteItemInInventory(int itemId, int inventoryId)
        {
            _itemService.DeleteItem(itemId);
            return Json(inventoryId);
        }
        /// <summary>
        /// 改变事件状态(清单中的事项) 
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public IActionResult ChangeStatueInInventory(int itemId)
        {
            _itemService.IsCompletionItems(itemId);
            return Ok();
        }

        /// <summary>
        /// 改变事件状态（所有未完成事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public IActionResult ChangeStatueInNotFinish(int itemId)
        {
            _itemService.IsCompletionItems(itemId);
            return RedirectToAction("ShowAllNoFinishItem");
        }

        /// <summary>
        /// 改变事件状态（所有完成事项中）
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public IActionResult ChangeStatueInFinish(int itemId)
        {
            _itemService.IsCompletionItems(itemId);
            return RedirectToAction("ShowAllFinishItem");
        }

        /// <summary>
        /// 将获得的Items集合的转换成ItemViewModels集合
        /// </summary>
        /// <param name="notFinishItems"></param>
        /// <returns></returns>
        private List<ItemViewModels> SaveInViewModels(List<Items> notFinishItems)
        {
            List<ItemViewModels> itemsView = new List<ItemViewModels>();
            foreach (var item in notFinishItems)
            {
                ItemViewModels itemView = new ItemViewModels()
                {
                    Content = item.Content,
                    CreateTime = item.CreateTime,
                    ItemId = item.ItemId,
                    UserId = item.UserId,
                    ItemOfInventoryId = item.ItemOfInventoryId,
                    TimeOfExpiration = item.TimeOfExpiration,
                    Mark = item.Mark,
                };
                itemsView.Add(itemView);
            }
            return itemsView;
        }

        /// <summary>
        /// 将获得的Inventory集合转换成InventoryViewModels
        /// </summary>
        /// <param name="inventoryList"></param>
        /// <returns></returns>
        private List<InventoryViewModel> SaveInViewModels(List<Inventorys> inventoryList)
        {
            List<InventoryViewModel> viewInventoryList = new List<InventoryViewModel>();
            foreach (var inventory in inventoryList)
            {
                var Inventory = new InventoryViewModel()
                {
                    InventoryName = inventory.InventoryName,
                    CreateTime = inventory.CreateTime,
                    InventoryId = inventory.InventoryId,
                    UserId = inventory.UserId,
                };
                viewInventoryList.Add(Inventory);
            }
            return viewInventoryList;
        }

        public IActionResult ChangeItem(int itemId)
        {

            ItemViewModels item = ChangeItemToItemViewModel(_itemService.GetItem(itemId));
            return View(item);
        }
        /// <summary>
        /// 修改完事件返回主界面
        /// </summary>
        /// <param name="content"></param>
        /// <param name="mark"></param>
        /// <param name="deadTime"></param>
        /// <param name="itemId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetBack(ItemViewModels item)
        {
            _itemService.ChangeItem(item.ItemId,item.Content,item.TimeOfExpiration,item.Mark);
            return RedirectToAction("ShowInventory", "Inventory");
        }
        /// <summary>
        /// 查找事项
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public IActionResult FindItem(string content)
        {
            List<ItemViewModels> _items = ChangeListItem
                (_itemService.SearchItem(content, (int)HttpContext.Session.GetInt32("userId")));
            if (_items.Count != 0)
            {
                return View("SearchReasult", _items);
            }
            else
                return View();
        }
        /// <summary>
        /// 在查找中删除事项
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteItemInSearch(int itemId)
        {
            _itemService.DeleteItem(itemId);
            return View("FindItemPart");
        }
        /// <summary>
        /// 展示添加事项的页面
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public IActionResult AddItem(int inventoryId)
        {
            return View(inventoryId);
        }
        /// <summary>
        /// 添加事项操作
        /// </summary>
        /// <param name="inventoryID"></param>
        /// <param name="content"></param>
        /// <param name="mark"></param>
        /// <param name="time"></param>
        public IActionResult ToAddItem(int inventoryId, string content, string mark, DateTime time)
        {
            _itemService.AddItem(content, time, mark, (int)HttpContext.Session.GetInt32("userId"), inventoryId);
            return RedirectToAction("ShowInventory", "Inventory");
        }

        /// <summary>
        /// 过期事项提醒
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public IActionResult ViewOverdueItemsRemind(string days)
        {
            List<ItemViewModels> overdueItems = SaveInViewModels(_itemService.GetWillOverdueItems((int)HttpContext.Session.GetInt32("userId"), days));
            return View(overdueItems);
        }
        /// <summary>
        /// 将item转化成itemViewModel
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private ItemViewModels ChangeItemToItemViewModel(Items item)
        {
            ItemViewModels itemModel = new ItemViewModels
            {
                Content = item.Content,
                ItemId = item.ItemId,
                UserId = item.UserId,
                State = item.State,
                CreateTime = item.CreateTime,
                IsOverdue = item.IsOverdue,
                ItemOfInventoryId = item.ItemOfInventoryId,
                Mark = item.Mark,
                TimeOfExpiration = item.TimeOfExpiration,

            };
            return itemModel;
        }

        /// <summary>
        /// 添加事项
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public List<ItemViewModels> ChangeListItem(List<Items> items)
        {
            List<ItemViewModels> _items = new List<ItemViewModels>();
            foreach (var tempItem in items)
            {
                _items.Add(ChangeItemToItemViewModel(tempItem));
            }
            return _items;
        }

        /// <summary>
        /// 将inventory转换成inventoryViewModel
        /// </summary>
        /// <param name="inventory"></param>
        /// <returns></returns>
        private InventoryViewModel ChangeInventory(Inventorys inventory)
        {
            InventoryViewModel inventoryModel = new InventoryViewModel
            {
                CreateTime = inventory.CreateTime,
                InventoryId = inventory.InventoryId,
                InventoryName = inventory.InventoryName,
                UserId = inventory.UserId,
            };
            return inventoryModel;
        }
    }
}